<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 小虎哥 <1105415366@qq.com>
 * Date: 2018-4-3
 */

namespace think\template\taglib\api;

use think\Db;

/**
 * 我的足迹列表
 */
class TagFootprint extends Base
{
    //初始化
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取我的足迹列表
     */
    public function getFootprintList($users = [], $page = 1, $pagesize = 10)
    {
        $where['a.users_id'] = $users['users_id'];
        $where['b.status'] = 1;
        $where['b.is_del'] = 0;
        $paginate = array(
            'page' => $page,
        );

        $pages = Db::name('users_footprint')
            ->alias('a')
            ->field('b.*,a.*')
            ->join('archives b', 'a.aid = b.aid', 'left')
            ->where($where)
            ->order('a.update_time desc')
            ->paginate($pagesize, false, $paginate);
        $list = $pages->toArray();

        if (!empty($list['data'])) {
            foreach ($list['data'] as $k => $v) {
                $v['litpic'] = $this->get_default_pic($v['litpic'], true);
                $v['date'] = date('Y-m-d', $v['update_time']);
                if (!empty($users['level'])){
                    $discount = Db::name('users_level')->where('level_id',$users['level'])->value('discount');
                    if (!empty($discount) && 100 != $discount){
                        $discount = strval($discount) / strval(100);
                        $v['users_price'] = floatval($v['users_price']) * floatval($discount);
                    }
                }
                $v['users_price'] = unifyPriceHandle($v['users_price']);
                $v['users_price_arr'] = explode('.',$v['users_price']);
                $list['data'][$k] = $v;
            }
            $date_arr = [];
            $today = date('Y-m-d');
            $yesteroday = date('Y-m-d',strtotime("-1 day"));
            foreach ($list['data'] as $k => $v) {
                $key = $v['date'];
                $date_arr[$key]['date'] = $v['date'];
                $date_arr[$key]['timestamp'] = strtotime($v['date']);
                if ($today == $v['date']){
                    $date_arr[$key]['date_title'] = '今日';
                }elseif ($yesteroday == $v['date']){
                    $date_arr[$key]['date_title'] = '昨日';
                }
                $date_arr[$key]['data'][] = $v;
            }
            rsort($date_arr);

            $list['data'] = $date_arr;
        }

        return $list;
    }

    //添加足迹
    public function addMyFootprintList($users = [], $aid = 0)
    {
        if (empty($aid) || empty($users['users_id'])) return false;
        $users_id = $users['users_id'];
        //查询标题模型缩略图信息
        $arc = Db::name('archives')
            ->field('aid,channel,typeid,title,litpic')
            ->find($aid);
        if (!empty($arc)) {
            $count = Db::name('users_footprint')->where([
                'users_id' => $users_id,
                'aid' => $aid,
            ])->count();

            if (empty($count)) {
                // 足迹记录条数限制
                $user_footprint_limit = config('global.user_footprint_limit');
                if (!$user_footprint_limit) {
                    $user_footprint_limit = 100;
                    config('global.user_footprint_limit', $user_footprint_limit);
                }
                $user_footprint_record = Db::name('users_footprint')->where(['users_id' => $users_id])->count("id");
                if ($user_footprint_record == $user_footprint_limit) {
                    Db::name('users_footprint')->where(['users_id' => $users_id])->order("update_time ASC")->limit(1)->delete();
                } elseif ($user_footprint_record > $user_footprint_limit) {
                    $del_count = $user_footprint_record - $user_footprint_limit + 1;
                    $del_ids = Db::name('users_footprint')->field("id")->where(['users_id' => $users_id])->order("update_time ASC")->limit($del_count)->select();
                    $del_ids = get_arr_column($del_ids, 'id');
                    Db::name('users_footprint')->where(['id' => ['IN', $del_ids]])->delete();
                }

                $arc['users_id'] = $users_id;
                $arc['add_time'] = getTime();
                $arc['update_time'] = getTime();
                Db::name('users_footprint')->add($arc);
            } else {
                Db::name('users_footprint')->where([
                    'users_id' => $users_id,
                    'aid' => $aid
                ])->update([
                    'update_time' => getTime(),
                ]);
            }
            return true;
        }
    }
}